﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XWG.JWBST.JWBSTEntity;
using XWG.JWBST.JWBSTBLL;
using XWG.Framework.MSException;
using JWBST.CommonClass;

namespace XWG.JWBST.JWBSTManagementApp.ZPXXCJ
{
    public partial class WebCollect : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Page.RegisterStartupScript("提示", "<script language=javascript>window.history.go(1);</script>");
                BindFraudtype();
            }
        }
        /// <summary>
        /// 绑定类型
        /// </summary>
        private void BindFraudtype()
        {
            var list = XWG.JWBST.JWBSTBLL.TbBstBsznBLL.GetDictionaryByCode("FRAUDTYPE");
            selFraudtype.DataSource = list;
            selFraudtype.DataTextField = "Description";
            selFraudtype.DataValueField = "Code";
            selFraudtype.DataBind();
            selFraudtype.Items.Insert(0, new ListItem("请选择", ""));
            selFraudtype.Value = "-1";


            SelSource.Items.Add(new ListItem("来电", "1"));
            SelSource.Items.Add(new ListItem("短信", "2"));
            SelSource.Items.Insert(0, new ListItem("请选择", ""));
            SelSource.Value = "-1";
        }



        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                labMsg.Text = "";
                if (txtyanzhengma.Value.FilteHtml().ToString() != Session["checkDX"].ToString())
                {
                    labMsg.Text = "请填写正确的验证码";
                    return;
                }

                TbBstFraudCollect mTbBstFraudCollect = new TbBstFraudCollect();
                mTbBstFraudCollect.Id = Guid.NewGuid().ToString(); //主键
                mTbBstFraudCollect.Receivedatetime = Convert.ToDateTime(texReceivedatetime.Value); //收到诈骗电话（短信）时间                
                mTbBstFraudCollect.Tel = texTel.Value.ToString().FilteHtml(); //来电显示的号码
                mTbBstFraudCollect.Source = Request.Form["SelSource"];
                if (!string.IsNullOrEmpty(mTbBstFraudCollect.Tel))
                {
                    string telNum = mTbBstFraudCollect.Tel.Substring(0, 3);
                    string carrieroperator = TbBstFraudCollectBLL.GetName(telNum);
                    //被举报号码所属运营商名称 来电显示号码为非手机号的名称为其他
                    if (!string.IsNullOrEmpty(carrieroperator))
                    {
                        mTbBstFraudCollect.Operatorsname = carrieroperator;
                    }
                    else
                    {
                        mTbBstFraudCollect.Operatorsname = "其他";
                    }
                    string carrieroperatorNum = "0";
                    switch (carrieroperator)
                    {
                        case "中国移动":
                            carrieroperatorNum = "1";
                            break;
                        case "中国联通":
                            carrieroperatorNum = "2";
                            break;
                        case "中国电信":
                            carrieroperatorNum = "3";
                            break;
                        case "":
                            carrieroperatorNum = "0";
                            break;
                        default:
                            break;
                    }
                    mTbBstFraudCollect.Operatorscode = carrieroperatorNum; //被举报号码所属运营商代码 1 移动号码 2 联通 3 电信 0 其他（来电显示号码为非手机号的）
                }
                mTbBstFraudCollect.Fraudtype = Convert.ToDecimal(Request.Form["selFraudtype"]); //诈骗名义代码 
                mTbBstFraudCollect.Msgcontent = texMsgcontent.Value.FilteHtml(); //短信内容
                mTbBstFraudCollect.Informername = texInformername.Value.FilteHtml(); //举报人姓名
                mTbBstFraudCollect.Informertel = texInformertel.Value.FilteHtml(); //举报人电话
                mTbBstFraudCollect.Informercompany = texInformercompany.Value.FilteHtml(); //举报人单位
                mTbBstFraudCollect.Createdate = DateTime.Now;
                mTbBstFraudCollect.Movesign = "1"; //同步标志
                mTbBstFraudCollect.Isextraction = "0"; //0 未推送  1已推送 2 已删除
                mTbBstFraudCollect.Fraudtypecontent = TbBstFraudCollectBLL.GetZPname(Request.Form["selFraudtype"].FilteHtml()); //诈骗名义具体内容
                mTbBstFraudCollect.Remark = texRemark.Value.FilteHtml(); //备注
                mTbBstFraudCollect.Ip = GetWebClientIp(); //举报人ip地址

                var mResultModel = TbBstFraudCollectBLL.Insert(mTbBstFraudCollect, "", false); //新建操作      
                if (mResultModel.ISSucceed)
                {

                    clertText();
                    Response.Buffer = true;
                    Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
                    Response.Expires = 0;
                    Response.CacheControl = "no-cache";
                    Page.RegisterStartupScript("提示", "<script language=javascript>alert(\"提交成功\");</script>");
                }
                else
                {
                    Page.RegisterStartupScript("提示", "<script language=javascript>alert(\"提交失败\")</script>");
                }

            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                Page.RegisterStartupScript("提示", "<script language=javascript>alert(\"提交失败\")</script>");
                //mResultModel.ErrorCode = -9999;
                //mResultModel.ISSucceed = false;
                //context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #region 获取web客户端ip
        /// <summary>
        /// 获取web客户端ip
        /// </summary>
        /// <returns></returns>
        public static string GetWebClientIp()
        {

            string userIP = "未获取用户IP";

            try
            {
                if (System.Web.HttpContext.Current == null
            || System.Web.HttpContext.Current.Request == null
            || System.Web.HttpContext.Current.Request.ServerVariables == null)
                    return "";

                string CustomerIP = "";

                //CDN加速后取到的IP simone 090805
                CustomerIP = System.Web.HttpContext.Current.Request.Headers["Cdn-Src-Ip"];
                if (!string.IsNullOrEmpty(CustomerIP))
                {
                    return CustomerIP;
                }

                CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];


                if (!String.IsNullOrEmpty(CustomerIP))
                    return CustomerIP;

                if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
                {
                    CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                    if (CustomerIP == null)
                        CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }
                else
                {
                    CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

                }

                if (string.Compare(CustomerIP, "unknown", true) == 0)
                    return System.Web.HttpContext.Current.Request.UserHostAddress;
                return CustomerIP;
            }
            catch { }

            return userIP;

        }
        #endregion

        public void clertText()
        {
            texTel.Value = "";
            selFraudtype.SelectedIndex = -1;
            texMsgcontent.Value = "";
            texReceivedatetime.Value = "";
            texInformername.Value = "";
            texInformertel.Value = "";
            texInformercompany.Value = "";
            texRemark.Value = "";
            txtyanzhengma.Value = "";
        }
    }
}